package org.gangcai.mac.shop.activity;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;

import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.bean.PullAuthoBean;
import org.gangcai.mac.shop.constants.Constant;
import org.gangcai.mac.shop.oberver.CommonObserver;
import org.gangcai.mac.shop.utils.AddressUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AuthoUserIdentifyActivity extends BaseAuthoActivity implements AddressUtils.CallBack {

    @BindView(R.id.titlebar)
    CommonTitleBar titlebar;
    @BindView(R.id.username)
    EditText username;
    @BindView(R.id.identifyNumber)
    EditText identifyNumber;
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.addressDetail)
    EditText addressDetail;
    @BindView(R.id.nextStep)
    Button nextStep;
    @BindView(R.id.addressFl)
    FrameLayout addressFl;
    private String defaultAddress;
    private BroadcastReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

        registerBrocastReceiver();
//        prompDialog.showLoading("请等待");
        getAuthoData("getrealname").subscribe(new CommonObserver<PullAuthoBean>() {
            @Override
            public void onSuccess(PullAuthoBean value) {
                username.setText(value.getInfo().getReal_name());
                identifyNumber.setText(value.getInfo().getId_number());
                address.setText(value.getInfo().getAddress());
                addressDetail.setText(value.getInfo().getAddress());
            }
        });
    }

    private void registerBrocastReceiver() {
        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                finish();
                receiver = null;
            }
        };
        LocalBroadcastManager.getInstance(this).registerReceiver(receiver, new IntentFilter(Constant.CLOSE_ACTIVITY));
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (receiver != null) {
            LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
        }
    }

    @Override
    protected String getTitleName() {
        return "实名认证";
    }

    @Override
    public int getcontentView() {
        return R.layout.activity_autho_user_identify;
    }

    @OnClick({R.id.addressFl, R.id.nextStep})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.addressFl:
                AddressUtils addressUtils = new AddressUtils(this);
                addressUtils.show(this);
                break;
            case R.id.nextStep:
                if (TextUtils.isEmpty(username.getText().toString())) {
                    ToastUtils.showShort("请输入您的姓名");
                } else if (TextUtils.isEmpty(identifyNumber.getText().toString())) {
                    ToastUtils.showShort("请输入您的身份证号码");
                } else if (!RegexUtils.isIDCard18(identifyNumber.getText().toString())) {
                    ToastUtils.showShort("请输入正确的身份证号码");
                } else if (TextUtils.isEmpty(defaultAddress)) {
                    ToastUtils.showShort("请选择你的地址");
                } else if (TextUtils.isEmpty(addressDetail.getText().toString())) {
                    ToastUtils.showShort("请输入您的详细地址");
                } else {

                    Bundle bundle = new Bundle();
                    bundle.putString("userName", username.getText().toString());
                    bundle.putString("identifyNumber", identifyNumber.getText().toString());
                    bundle.putString("city", defaultAddress);
                    bundle.putString("addressDetail", addressDetail.getText().toString());
                    ActivityUtils.startActivity(bundle, this, AuthoUserIdentifyImageActivity.class);
                }

                break;
        }
    }

    @Override
    public void callBack(String sheng, String shi, String qu) {
        defaultAddress = sheng + shi + qu;
        address.setText(defaultAddress);
    }

    public static void close(Application application) {
        LocalBroadcastManager.getInstance(application).sendBroadcast(new Intent(Constant.CLOSE_ACTIVITY));
    }
}
